var prefix = "/sys/dict";
$(document).ready(function () {
    validateRule();
    selectLoad();
});

$.validator.setDefaults({
    submitHandler: function () {
        save();
    }
});

function save() {
    $.ajax({
        cache: true,
        type: "POST",
        url: "/sys/dict/save",
        data: $('#dictForm').serialize(), // 你的formid
        async: false,
        error: function () {
            parent.layer.alert("网络超时");
        },
        success: function (data) {
            if (data.code === 0) {
                parent.layer.msg("操作成功");
                parent.reLoad();
                var index = parent.layer.getFrameIndex(window.name);
                parent.layer.close(index);

            } else {
                parent.layer.alert(data.msg)
            }

        }
    });

}

function validateRule() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#dictForm").validate({
        rules: {
            name: {
                required: true
            },
            type: {
                required: true
            }
        },
        messages: {
            name: {
                required: icon + "请输入标签名称"
            },
            type: {
                required: icon + "请选择数据类型"
            }
        }
    })
}

function selectLoad() {
    var html = "";
    $.ajax({
        url: '/sys/dict/type',
        success: function (data) {
            //加载数据
            for (var i = 0; i < data.length; i++) {
                html += '<option value="' + data[i].typeValue + '">' + data[i].typeName + '</option>'
            }
            $(".chosen-select").append(html);
            $(".chosen-select").chosen({
                maxHeight: 200
            });
            //点击事件
            $('.chosen-select').on('change', function (e, params) {
                $('#type').val(params.selected);
            });
        }
    });
}